package com.serviceImpl;

import java.io.File;
import java.io.FileInputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.dao.manager.QrcodeMapper;
import com.pojo.Qrcode;
import com.service.QrcodeService;
import com.utils.QRCodeUtil;
import com.utils.UUIDUtils;
import com.utils.UploadUtils;

@Service
public class QrcodeServiceImp implements QrcodeService {

	@Resource
	private QrcodeMapper qrcodeMapper;
	@Value("${img.upload.path}")
	private String imgUploadPath;
	@Value("${img.access.path}")
	private String imgAccessPath;

	@Override
	public int addQrcodeInfo(Map<String, String> paramsMap, MultipartFile[] pxtimg, MultipartFile[] jxtimg) {
		try {
			// 定义一个uuid
			String uuid = UUIDUtils.getUUID();
			// 生成二维码
			String path = paramsMap.get("pathString") + File.separator + uuid + ".JPG";
			QRCodeUtil.encode(paramsMap.get("contextPath") + "/qrcode/getQrcodeInfo?uuid=" + uuid, "", path);
			// 把file转换为MultipartFile, 并调用七牛进行上传
			File ewmFile = new File(path);
			FileInputStream input = new FileInputStream(ewmFile);
			MultipartFile multipartFile = new MockMultipartFile("file", uuid + ".JPG", "text/plain",
					IOUtils.toByteArray(input));
			MultipartFile[] ewmfiles = new MultipartFile[] { multipartFile };
			List<String> ewmPaths = UploadUtils.uploadQiniuImg(ewmfiles, imgUploadPath, imgAccessPath);
			// MultipartFile multipartFile = new MockMultipartFile(file.getName(),
			// inputStream);
			// UploadUtils.uploadQiniuImg(imgfiles, imgUploadPath, imgAccessPath);
			List<String> pxtimgPaths = UploadUtils.uploadQiniuImg(pxtimg, imgUploadPath, imgAccessPath);
			List<String> jxtimgPaths = UploadUtils.uploadQiniuImg(jxtimg, imgUploadPath, imgAccessPath);
			Qrcode entity = new Qrcode();
			if (pxtimgPaths != null && !pxtimgPaths.isEmpty()) {
				entity.setPxtimg(pxtimgPaths.get(0));
			} else {
				entity.setPxtimg("暂无图片");
			}
			if (jxtimgPaths != null && !jxtimgPaths.isEmpty()) {
				entity.setJxtimg(jxtimgPaths.get(0));
			} else {
				entity.setJxtimg("暂无图片");
			}
			if (ewmPaths != null && !ewmPaths.isEmpty()) {
				String ewmPath = ewmPaths.get(0);
				entity.setEwmimg(ewmPath);
			}
			entity.setUuid(uuid);
			entity.setName(paramsMap.get("name"));
			entity.setPxtcode(paramsMap.get("pxtcode"));
			entity.setJxtcode(paramsMap.get("jxtcode"));
			entity.setCreatetime(new Date());
			qrcodeMapper.addQrcodeInfo(entity);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public List<Map<String, String>> getQrcodeInfo(Map<String, String> paramsMap) {
		String page = paramsMap.get("page");
		if (page == null || "".equals(page)) {
			page = "1";
		}
		String num = paramsMap.get("num");
		if (num == null || "".equals(num)) {
			num = "10";
			paramsMap.put("num", num);
		}
		int start = (Integer.parseInt(page) - 1) * Integer.parseInt(num);
		paramsMap.put("start", start + "");
		return qrcodeMapper.getQrcodeInfo(paramsMap);
	}

	@Override
	public int deleteQrcodeInfo(Map<String, String> paramsMap) {
		try {
			return qrcodeMapper.deleteQrcodeInfo(paramsMap);
		} catch (Exception e) {
			e.printStackTrace();
			return -2;
		}
	}

	@Override
	@Transactional
	public int updateQrcodeInfo(Map<String, String> paramsMap, MultipartFile[] pxtimg, MultipartFile[] jxtimg) {
		try {
			// 定义一个uuid
			String uuid = paramsMap.get("uuid");
			System.out.println(uuid);// 357b8a127f3c4cbea4bbfc5ddb1bc8c2
			// 生成二维码
			String path = paramsMap.get("pathString") + File.separator + uuid + ".JPG";
			QRCodeUtil.encode(paramsMap.get("contextPath") + "/qrcode/getQrcodeInfo?uuid=" + uuid, "", path);
			// 把file转换为MultipartFile, 并调用七牛进行上传
			File ewmFile = new File(path);
			FileInputStream input = new FileInputStream(ewmFile);
			MultipartFile multipartFile = new MockMultipartFile("file", uuid + ".JPG", "text/plain",
					IOUtils.toByteArray(input));
			MultipartFile[] ewmfiles = new MultipartFile[] { multipartFile };
			List<String> ewmPaths = UploadUtils.uploadQiniuImg(ewmfiles, imgUploadPath, imgAccessPath);

			List<String> pxtimgPaths = UploadUtils.uploadQiniuImg(pxtimg, imgUploadPath, imgAccessPath);
			List<String> jxtimgPaths = UploadUtils.uploadQiniuImg(jxtimg, imgUploadPath, imgAccessPath);
			Qrcode entity = new Qrcode();
			if (pxtimgPaths != null && !pxtimgPaths.isEmpty()) {
				entity.setPxtimg(pxtimgPaths.get(0));
			} else {
				entity.setPxtimg("暂无图片");
			}
			if (jxtimgPaths != null && !jxtimgPaths.isEmpty()) {
				entity.setJxtimg(jxtimgPaths.get(0));
			} else {
				entity.setJxtimg("暂无图片");
			}
			if (ewmPaths != null && !ewmPaths.isEmpty()) {
				String ewmPath = ewmPaths.get(0);
				entity.setEwmimg(ewmPath);
			}
			entity.setUuid(uuid);
			entity.setName(paramsMap.get("name"));
			entity.setPxtcode(paramsMap.get("pxtcode"));
			entity.setJxtcode(paramsMap.get("jxtcode"));
			entity.setCreatetime(new Date());
			qrcodeMapper.updateQrcodeInfo(entity);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public List<Map<String, String>> getQrcodeInfoByUuid(Map<String, String> paramsMap) {
		return qrcodeMapper.getQrcodeInfoByUuid(paramsMap.get("uuid"));
	}

}
